package com.daikin.ktmustwrite.ui.fragment.publicNumber

import android.os.Bundle
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ConvertUtils
import com.daikin.ktmustwrite.R
import com.daikin.ktmustwrite.app.base.BaseFragment
import com.daikin.ktmustwrite.app.ext.*
import com.daikin.ktmustwrite.app.weight.recycleview.DefineLoadMoreView
import com.daikin.ktmustwrite.app.weight.recycleview.SpaceItemDecoration
import com.daikin.ktmustwrite.databinding.IncludeListBinding
import com.daikin.ktmustwrite.ui.adapter.AriticleAdapter
import com.daikin.ktmustwrite.viewmodel.request.RequestPublicNumberViewModel
import com.daikin.ktmustwrite.viewmodel.state.PublicNumberViewModel
import com.kingja.loadsir.core.LoadService
import com.yanzhenjie.recyclerview.SwipeRecyclerView
import kotlinx.android.synthetic.main.include_recyclerview.*
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel

class PublicChildFragment: BaseFragment<PublicNumberViewModel, IncludeListBinding>() {

    //适配器
    private val articleAdapter: AriticleAdapter by lazy { AriticleAdapter(arrayListOf()) }

    //界面状态管理者
    private lateinit var loadsir: LoadService<Any>

    //recyclerview的底部加载view 因为在首页要动态改变他的颜色，所以加了他这个字段
    private lateinit var footView: DefineLoadMoreView

    //该项目对应的id
    private var cid = 0

    //请求viewmodel
    private val requestPublicNumberViewModel: RequestPublicNumberViewModel by viewModels()

    override fun layoutId(): Int {
        return R.layout.include_list
    }

    override fun initView(savedInstanceState: Bundle?) {
        arguments?.let {
            cid = it.getInt("cid")
        }
        //状态页配置
        loadsir = loadServiceInit(swipeRefresh) {
            //点击重试时触发的操作
            loadsir.showLoading()
            requestPublicNumberViewModel.getPublicData(true, cid)
        }
        //初始化recyclerView
        recyclerView.init(LinearLayoutManager(context), articleAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, ConvertUtils.dp2px(8f)))
            footView = it.initFooter(SwipeRecyclerView.LoadMoreListener {
                //触发加载更多时请求数据
                requestPublicNumberViewModel.getPublicData(false, cid)
            })
        }

        //初始化 SwipeRefreshLayout
        swipeRefresh.init {
            //触发刷新监听时请求数据
            requestPublicNumberViewModel.getPublicData(true, cid)
        }

        articleAdapter.run {

        }
    }

    override fun lazyLoadData() {
        loadsir.showLoading()
        requestPublicNumberViewModel.getPublicData(true,cid)
    }

    override fun createObserver() {
        requestPublicNumberViewModel.publicDataState.observe(viewLifecycleOwner, Observer {
            //设值 新写了个拓展函数，搞死了这个恶心的重复代码
            loadListData(it, articleAdapter, loadsir, recyclerView, swipeRefresh)
        })
}
    companion object {
        fun newInstance(cid: Int): PublicChildFragment {
            val args = Bundle()
            args.putInt("cid", cid)
            val fragment = PublicChildFragment()
            fragment.arguments = args
            return fragment
        }
    }
}